package com.hmdp.utils;

import org.springframework.data.redis.core.StringRedisTemplate;

import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;

@Component
public class RedisWorker {
    private static final Long BEGIN_TIME=1735689600L;
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    public Long generateId(String prefixKey) {
        //生成时间戳
        LocalDateTime now = LocalDateTime.now();
        long second = now.toEpochSecond(ZoneOffset.UTC);
        long timestamp = second - BEGIN_TIME;
        //生成序列号
        String format = now.format(DateTimeFormatter.ofPattern("yyyy:MM:dd"));
        Long count=stringRedisTemplate.opsForValue().increment("icr:"+prefixKey+":"+format);
        return timestamp<<32|count;
    }
}
